home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / m / floor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-16  |  4.2 KB  |  162 lines

  1. /*
  2.  * Copyright (c) 1985 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  *
  12.  * All recipients should regard themselves as participants in an ongoing
  13.  * research project and hence should feel obligated to report their
  14.  * experiences (good or bad) with these elementary function codes, using
  15.  * the sendbug(8) program, to the authors.
  16.  */
  17.  
  18. #ifndef lint
  19. static char sccsid[] = "@(#)floor.c    5.3 (Berkeley) 5/21/88";
  20. #endif /* not lint */
  21.  
  22. #if defined(vax)||defined(tahoe)
  23. #ifdef vax
  24. #define _0x(A,B)    0x/**/A/**/B
  25. #else    /* vax */
  26. #define _0x(A,B)    0x/**/B/**/A
  27. #endif    /* vax */
  28. static long Lx[] = {_0x(0000,5c00),_0x(0000,0000)};    /* 2**55 */
  29. #define L *(double *) Lx
  30. #else    /* defined(vax)||defined(tahoe) */
  31. #ifdef __STDC__
  32. volatile static double L = 2 * 4503599627370496.0E0;        /* 2**53 */
  33. #else
  34. static double L = 2 * 4503599627370496.0E0;        /* 2**53 */
  35. #endif
  36. #endif    /* defined(vax)||defined(tahoe) */
  37. #ifdef ds3100
  38. #define MAXLONG 2147483648.0E0
  39. #endif
  40.  
  41. /*
  42.  * floor(x) := the largest integer no larger than x;
  43.  * ceil(x) := -floor(-x), for all real x.
  44.  *
  45.  * Note: Inexact will be signaled if x is not an integer, as is
  46.  *    customary for IEEE 754.  No other signal can be emitted.
  47.  */
  48. double
  49. floor(x)
  50. double x;
  51. {
  52.     double y,ceil();
  53.  
  54.     if (
  55. #if !defined(vax)&&!defined(tahoe)
  56.         x != x ||    /* NaN */
  57. #endif    /* !defined(vax)&&!defined(tahoe) */
  58.         x >= L)        /* already an even integer */
  59.         return x;
  60.     else if (x < (double)0)
  61.         return -ceil(-x);
  62. /*
  63.  * This will ensure floor() is computed correctly at least for values of x
  64.  * within the range of long's.
  65.  * The algorithm used otherwise gives bogus results on ds3100.
  66.  * E.g.: floor(1.0) = 0.0, floor(-3.0) = -3.0, etc.
  67.  */
  68. #ifdef ds3100
  69.     else if ( x < MAXLONG )
  70.         return (long)x;
  71. #endif
  72.     else {            /* now 0 <= x < L */
  73.         y = L+x;        /* destructive store must be forced */
  74.         y -= L;            /* an integer, and |x-y| < 1 */
  75.         return x < y ? y-(double)1 : y;
  76.     }
  77. }
  78.  
  79. double
  80. ceil(x)
  81. double x;
  82. {
  83.     double y,floor();
  84.  
  85.     if (
  86. #if !defined(vax)&&!defined(tahoe)
  87.         x != x ||    /* NaN */
  88. #endif    /* !defined(vax)&&!defined(tahoe) */
  89.         x >= L)        /* already an even integer */
  90.         return x;
  91.     else if (x < (double)0)
  92.         return -floor(-x);
  93. #ifdef ds3100
  94.     else if ( x < MAXLONG )
  95.         return x == (long)x ? x : (long)x + 1.0;
  96. #endif
  97.     else {            /* now 0 <= x < L */
  98.         y = L+x;        /* destructive store must be forced */
  99.         y -= L;            /* an integer, and |x-y| < 1 */
  100.         return x > y ? y+(double)1 : y;
  101.     }
  102. }
  103.  
  104. #ifndef national            /* rint() is in ./NATIONAL/support.s */
  105. /*
  106.  * algorithm for rint(x) in pseudo-pascal form ...
  107.  *
  108.  * real rint(x): real x;
  109.  *    ... delivers integer nearest x in direction of prevailing rounding
  110.  *    ... mode
  111.  * const    L = (last consecutive integer)/2
  112.  *       = 2**55; for VAX D
  113.  *       = 2**53; for IEEE 754 Double
  114.  * real    s,t;
  115.  * begin
  116.  *     if x != x then return x;        ... NaN
  117.  *     if |x| >= L then return x;        ... already an integer
  118.  *     s := copysign(L,x);
  119.  *     t := x + s;                ... = (x+s) rounded to integer
  120.  *     return t - s
  121.  * end;
  122.  *
  123.  * Note: Inexact will be signaled if x is not an integer, as is
  124.  *    customary for IEEE 754.  No other signal can be emitted.
  125.  */
  126. double
  127. rint(x)
  128. double x;
  129. {
  130.     double s,t,one = 1.0,copysign();
  131. #if !defined(vax)&&!defined(tahoe)
  132.     if (x != x)                /* NaN */
  133.         return (x);
  134. #endif    /* !defined(vax)&&!defined(tahoe) */
  135.     if (copysign(x,one) >= L)        /* already an integer */
  136.         return (x);
  137.     s = copysign(L,x);
  138.     t = x + s;                /* x+s rounded to integer */
  139.     return (t - s);
  140. }
  141. #else /* !national */
  142. /* 
  143.  * The ds3100 defines national but doesn't have the auxiliary routine
  144.  * mentioned above.
  145.  */
  146. #ifdef ds3100
  147. double
  148. rint(x)
  149. double x;
  150. {
  151.     register double fx = floor(x);
  152.  
  153.     if ( x - fx < 0.5 /* floor is the nearest int */ ||
  154.          x - fx == 0.5 && fx == 2.0 * floor(fx / 2.0)
  155.             /* floor is the nearest EVEN int */ )
  156.         return fx;
  157.     else
  158.         return fx + 1.0;
  159. }
  160. #endif /* ds3100 */
  161. #endif    /* not national */
  162.